package isoption

import (
	"net/http"
	"strconv"
	"time"

	"gitee.com/ispace/core/controller/fileTransferController"
	"gitee.com/ispace/core/controller/identityController"
	"gitee.com/ispace/core/infrastructure/common/dto"
	"github.com/gin-gonic/gin"
)

func ApiOption(host *gin.Engine) {
	api_host := host.Group("/api")

	// #section 1: identity

	// step 1:
	api_host.POST("/GetToken", func(c *gin.Context) {
		identityController := identityController.New()
		// step 1.2： 获取请求信息
		var request struct {
			Name     string `json:"name"`
			Password string `json:"password"`
		}

		if err := c.ShouldBindJSON(&request); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		c.JSON(200, identityController.GetToken(request.Name, request.Password))
	})

	// step 2：
	api_host.GET("/VerifyToken", func(c *gin.Context) {
		identityController := identityController.New()
		// step 2.1： 获取请求信息
		token, e2 := c.GetQuery("token")
		// step 3：校验
		if !e2 {
			c.JSON(200, gin.H{"message": "参数不正确，请重试"})
			return
		}
		c.JSON(200, identityController.Verify(token))
	})

	// step 2：
	api_host.GET("/Datetime", func(c *gin.Context) {
		result := dto.ResultDto[time.Time]{}
		result.Code = 200
		result.Data = time.Now()
		result.Success = true
		result.Message = "success"
		c.JSON(200, result)
	})

	// #section fileTransfer
	ft_host := api_host.Group("/fileTransfer")

	//  step 1：
	ft_host.GET("/file/*filePath", func(ctx *gin.Context) {
		// 从URL参数中获取文件名
		filePath := ctx.Param("filePath")
		ft := fileTransferController.New(ctx)

		result := ft.File(filePath)

		if !result.Success {
			ctx.JSON(result.Code, gin.H{"error": result.Message})
			return
		}

		// 使用Gin内置的File方法高效传输文件
		ctx.File(result.Data)
	})

	// step 2:
	ft_host.POST("/fileUpload", func(c *gin.Context) {

		// step init:
		result := dto.ResultDto[string]{}

		// 获取表单参数
		file, err := c.FormFile("file")
		if err != nil {
			result.Code = 500
			result.Message = "文件上传失败: " + err.Error()
			result.Success = false
			c.JSON(200, result)
			return
		}

		path := c.PostForm("path")
		if path == "" {
			result.Code = 500
			result.Message = "路径参数不能为空"
			result.Success = false
			c.JSON(200, result)
			return
		}

		ft := fileTransferController.New(c)

		result = ft.FileUpload(path, file)

		if !result.Success {
			c.JSON(result.Code, gin.H{"error": result.Message})
			return
		}
	})

	// step 3： BigFileUpload
	// step 3.1：init
	ft_host.POST("/bigFileUpload/init", func(c *gin.Context) {

		// step init:
		var request struct {
			Size int `json:"size"`
		}

		if err := c.ShouldBindJSON(&request); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// step 2：处理
		ft := fileTransferController.New(c)
		result := ft.BigFileUploadInit(request.Size)

		if !result.Success {
			c.JSON(result.Code, gin.H{"error": result.Message})
		}

		c.JSON(200, result)
	})

	// step 3.2：chunk
	ft_host.POST("/bigFileUpload/chunk", func(c *gin.Context) {
		// step 1：file
		file, err := c.FormFile("chunk")
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		// step 2：index
		indexStr := c.PostForm("index")
		index, err := strconv.Atoi(indexStr)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// step 3：key
		key := c.PostForm("key")

		// step 2：处理
		ft := fileTransferController.New(c)
		result := ft.BigFileUploadChunk(key, index, file)

		if !result.Success {
			c.JSON(result.Code, gin.H{"error": result.Message})
		}

		c.JSON(200, result)
	})

	// step 3.3：complete
	ft_host.POST("/bigFileUpload/complete", func(c *gin.Context) {

		// step init:
		var request struct {
			Key          string `json:"key"`
			Name         string `json:"name"`
			Size         int    `json:"size"`
			ChunkSize    int    `json:"chunkSize"`
			ChunkCount   int    `json:"chunkCount"`
			Path         string `json:"path"`
			RepeatPolicy int    `json:"repeatPolicy"`
		}

		if err := c.ShouldBindJSON(&request); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// step 2：处理
		ft := fileTransferController.New(c)
		result := ft.BigFileUploadComplete(request.Key, request.Name, request.Size, request.ChunkSize, request.ChunkCount, request.Path, request.RepeatPolicy)

		if !result.Success {
			c.JSON(result.Code, gin.H{"error": result.Message})
		}

		c.JSON(200, result)
	})

	// step 3.4：cancel
	ft_host.POST("/bigFileUpload/cancel", func(c *gin.Context) {
		// step init:
		var request struct {
			Key string `json:"key"`
		}

		if err := c.ShouldBindJSON(&request); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// step 2：处理
		ft := fileTransferController.New(c)
		result := ft.BigFileUploadCancel(request.Key)

		if !result.Success {
			c.JSON(result.Code, gin.H{"error": result.Message})
		}

		c.JSON(200, result)
	})

}
